﻿using PickingGuide.Business;
using PickingGuide.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PickingGuideForm
{
    public partial class FrmPartsCodeSet : Form
    {
        PartsBLL partsBLL = new PartsBLL();
        PartsModel _Parts;
        public Action RefreshUiEvent;

        public FrmPartsCodeSet()
        {
            InitializeComponent();
        }

        public FrmPartsCodeSet(PartsModel parts)
        {
            InitializeComponent();
            _Parts = parts;
            txtCarCode.Text = parts.CarCode;
            txtPartsCode.Text = parts.PartsCode;
            btnSave.Text = "修改";
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (_Parts == null)
            {
                var model = partsBLL.GetModel(txtCarCode.Text, txtPartsCode.Text);
                if (model != null)
                {
                    MessageBox.Show("已存在相同的品番配置，不能重复！");
                    return;
                }
                _Parts = new PartsModel();
                _Parts.CarCode = txtCarCode.Text;
                _Parts.PartsCode = txtPartsCode.Text;
                var result = partsBLL.Add(_Parts);
                if (result.IsSuccess)
                {
                    MessageBox.Show("添加品番成功");
                    RefreshUiEvent?.BeginInvoke(null, null);
                    this.Close();
                }
                else
                    MessageBox.Show("添加品番失败：" + result.Dsca);
            }
            else
            {
                _Parts.CarCode = txtCarCode.Text;
                _Parts.PartsCode = txtPartsCode.Text;
                var result = partsBLL.Update(_Parts);
                if (result.IsSuccess)
                {
                    MessageBox.Show("修改品番成功");
                    RefreshUiEvent?.BeginInvoke(null, null);
                    this.Close();
                }
                else
                    MessageBox.Show("修改品番失败：" + result.Dsca);
            }
        }
    }
}
